package cn.edu.thsis.servlet;

import java.io.IOException;
import java.util.List;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.thsis.pojo.User;
import cn.edu.thsis.pojo.student;
import cn.edu.thsis.pojo.teacher;
import cn.edu.thsis.service.UserService;
import cn.edu.thsis.service.impl.UserServiceImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
	//获取servlet层对象
	UserService us = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resq) throws ServletException, IOException {
    	//设置请求编码格式
    	req.setCharacterEncoding("utf-8");
    	//设置响应编码格式
    	resq.setContentType("text/html;charset=utf-8");
    	//获取操作符
    	String oper = req.getParameter("oper");
    	if("login".equals(oper)) {//调用登录处理方法
    		try {
				checkUserLogin(req,resq);
			} catch (SQLException e) {
				e.printStackTrace();
			}
    		}else if("out".equals(oper)) {//调用退出功能
    		userOut(req,resq);
    		}else if("pwd".equals(oper)) {//调用密码修改功能
    			try {
					userChangePwd(req,resq);
				} catch (SQLException e) {
					e.printStackTrace();
				}
    		}else if ("finduser".equals(oper)) {//根据不同用户类型进行不同操作
				findUser(req,resq);
			}
    		else if("showtea".equals(oper)) {
    			//调用查询教师信息功能
    			try {
					teaShow(req,resq);
				} catch (SQLException e) {
					e.printStackTrace();
				}
    		}
    	}
 //根据不同用户类型进行不同操作
	private void findUser(HttpServletRequest req, HttpServletResponse resq) throws IOException {
		HttpSession session = req.getSession();
		String uid = ((User) session.getAttribute("user")).getUid();
		String type = (String) session.getAttribute("type");
		student student = new student();
		teacher teacher = new teacher();
		if (type.equals("student")) {
			try {
				student = us.findStuService(uid, type);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			session.setAttribute("user", student);
		}else {
			try {
				teacher = us.fidTeaService(uid, type);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			session.setAttribute("user", teacher);
		}
		resq.sendRedirect("student/profile.jsp");
	}

	private void teaShow(HttpServletRequest req, HttpServletResponse resq) throws SQLException, ServletException, IOException {
	    List<teacher> lu = us.teaShowService();
	    if (lu!=null) {
	    	//将用户查询数据存储到request对象
			req.setAttribute("lu", lu);
			//请求转发
			req.getRequestDispatcher("student/tea_list.jsp").forward(req, resq);
		}
	}
	
//用户修改密码
	private void userChangePwd(HttpServletRequest req, HttpServletResponse resq) throws IOException, SQLException {
		//获取数据
		String newPwd = req.getParameter("newPwd");
		//从Session中获取用户信息
		User u = (User) req.getSession().getAttribute("user");
		HttpSession session = req.getSession();
    	String type1 = (String)session.getAttribute("type");
		String uid = u.getUid();
		//处理请求
		   //调用service处理
		int index = us.userChangePwdService(newPwd,uid,type1);
		if (index>0) {
			//重定向到登录页面
			session.setAttribute("pwd", "true");
			resq.sendRedirect("login.jsp");
			
		}
	}

//用户退出 
	private void userOut(HttpServletRequest req, HttpServletResponse resq) throws IOException {
		//获取session对象
		HttpSession hs = req.getSession();
		//强制销毁session
		hs.invalidate();
		//重定向到登录页面
		resq.sendRedirect("login.jsp");
	}
	
	//处理登录
	private void checkUserLogin(HttpServletRequest req, HttpServletResponse resq) throws IOException, ServletException, SQLException {
		String uid = req.getParameter("uid");
		String pwd = req.getParameter("pwd");
		String type = req.getParameter("type");
		User u = us.checkUserLoginService(uid,pwd,type);
		if(u!=null) {
			//获取session对象
			HttpSession hs = req.getSession();
			//将用户数据存储到session中
			hs.setAttribute("user", u);
			hs.setAttribute("type", type);
			//重定向
				goo(req,resq,type);
		}else {
			//添加标识符到request
			req.setAttribute("flag",0);
			//请求转发
			req.getRequestDispatcher("login.jsp").forward(req, resq);
			return;
			}
		}

	private void goo(HttpServletRequest req, HttpServletResponse resq, String type) throws ServletException, IOException {
		//转向学生功能界面
				if(type.equals("student")) {
					resq.sendRedirect("student/index.jsp");
					return;
				}
				//转向教师功能界面
				if(type.equals("teacher")) {
					resq.sendRedirect("teacher/tea_index.jsp");
					return;
				}
				//转向管理员功能界面
				if(type.equals("admin")) {
					resq.sendRedirect("admin/admin_index.jsp");
					return;
				}
	}
    }
